package twitter;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class ServerView extends JFrame implements ActionListener{
	
	private static final long serialVersionUID = 1L;

	JButton startButton;
	JButton stopButton;
	JLabel infoLabel;
	JLabel userLabel;
	Container buttons;
	Container connect;
	Container labels;
	JTextField userName; 
	JTextField port;
	JTextField dbIp;
	JPasswordField passwordField;

	TwitterServer tServer = new TwitterServer(this);
	boolean isOn = false;
	ServerView view = this;

	
	public void startView(){
		this.setLayout(new GridLayout(3,1));
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		this.setTitle("TwitterServer");
		
		buttons = new Container();
		connect = new Container();
		labels = new Container();
		buttons.setLayout(new FlowLayout(1,15,20));
		connect.setLayout(new GridLayout(4,1));
		labels.setLayout(new GridLayout(2,1));
		
		startButton = new JButton("start");
		startButton.addActionListener(this);
		stopButton = new JButton("stop");
		stopButton.addActionListener(this);
		
		JLabel portLabel = new JLabel("  port:");
		port = new JTextField("5698",3);
		
		JLabel nameLabel = new JLabel("  user name:");
		userName = new JTextField("root",4);
		
		JLabel passwordLabel = new JLabel("  password:");
		passwordField = new JPasswordField("dbpro",8);
		passwordField.addActionListener(this);
		
		JLabel urlLabel = new JLabel("  DB Address:");
		dbIp = new JTextField("localhost",16);
		
		infoLabel = new JLabel("");
		userLabel = new JLabel("Number of updated users: "+ TwitterServer.numUpdatedUser);
		
		
		connect.add(urlLabel);
		connect.add(dbIp);
		connect.add(portLabel);
		connect.add(port);
		connect.add(nameLabel);
		connect.add(userName);
		connect.add(passwordLabel);
		connect.add(passwordField);
		
		buttons.add(startButton);
		buttons.add(stopButton);
		
		labels.add(infoLabel);
		labels.add(userLabel);
		
		this.add(buttons);
		this.add(connect);
		this.add(labels);
		
		this.pack();
		this.setVisible(true);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource().equals(startButton)){
			if(!isOn){
				Thread t = new Thread(){
					public void run(){
						try{
						tServer.port = Integer.parseInt(view.port.getText());
						tServer.dbAddress = view.dbIp.getText();
						tServer.dbUserName = userName.getText();
						tServer.dbPassword = new String(passwordField.getPassword());
						tServer.connect();
						}catch(NumberFormatException e){
							infoLabel.setText("please insert a valid number");
							isOn = false; 
						}
					}
				};
				t.start();
			}else{
				infoLabel.setText("already on");
			}
		}
		else if(e.getSource().equals(stopButton)){
			System.exit(EXIT_ON_CLOSE);
		}
	}
	
	public static void main(String[] args) {
		ServerView sView = new ServerView();
		sView.startView();
	}
}
